package com.hrp.book.ui.main

import android.content.res.ColorStateList
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.graphics.drawable.DrawableCompat
import com.hrp.book.R
import com.hrp.book.base.BaseActivity
import com.hrp.book.base.BaseFragment
import com.hrp.book.base.BaseViewModel
import com.hrp.book.databinding.ActivityMainBinding
import com.hrp.book.ui.bookcity.BookCityFragment
import com.hrp.book.ui.bookshelf.BookShelfFragment
import com.hrp.book.ui.user.UserFragment
import com.hrp.book.utils.DisplayUtils
import me.yokeyword.fragmentation.ISupportFragment

class MainActivity : BaseActivity<ActivityMainBinding, BaseViewModel>() {


    private lateinit var bookShelfState: State
    private lateinit var bookCityState: State
    private lateinit var userState: State

    private lateinit var showState: State
    

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun getContainerId(): Int {
        return R.id.container
    }

    override fun hasViewModel(): Boolean {
        return false
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        bookShelfState =
            State(BookShelfFragment.newInstance(), binding.ivBookShelf, binding.tvBookShelf, true)
        bookCityState =
            State(BookCityFragment.newInstance(), binding.ivBookCity, binding.tvBookCity)

        userState =
            State(UserFragment.newInstance(), binding.ivUser, binding.tvUser)
        showState = bookShelfState
        loadMultipleRootFragment(
            0,
            bookShelfState.fragment,
            bookCityState.fragment,
            userState.fragment
        )
    }

    private fun toggle(state: State) {
        if (showState === state) {
            return
        }
        showHideFragment(state.fragment, showState.fragment)
        state.isSelected = true
        showState.isSelected = false
        showState = state
    }

    fun bookShelf(view: View) {
        if (isRepeatClick) {
            return
        }
        toggle(bookShelfState)
    }

    fun bookCity(view: View) {
        if (isRepeatClick) {
            return
        }
        toggle(bookCityState)
    }

    fun user(view: View) {
        if (isRepeatClick) {
            return
        }
        toggle(userState)
    }


    class State(
        val fragment: BaseFragment<*, *>,
        private val imageView: ImageView,
        private val textView: TextView,
        selected: Boolean = false
    ) {

        companion object {
            private val SELECT_STATES = arrayOf(
                intArrayOf(android.R.attr.state_selected),
                intArrayOf(-android.R.attr.state_selected),
            )

            private val SELECT_COlOS: IntArray by lazy {
                intArrayOf(
                    DisplayUtils.getColor(R.color.main_color),
                    DisplayUtils.getColor(R.color.default_color)
                )
            }
        }

        var isSelected: Boolean = false
            set(value) {
                field = value
                textView.isSelected = value
                imageView.isSelected = value
            }

        init {
            textView.setTextColor(ColorStateList(SELECT_STATES, SELECT_COlOS))
            imageView.drawable?.let {
                DrawableCompat.setTintList(it, ColorStateList(SELECT_STATES, SELECT_COlOS))
            }
            isSelected = selected
        }
    }

    override fun loadRootFragment(toFragment: ISupportFragment) {
        super.loadRootFragment(R.id.full_screen_container, toFragment)
    }
}